//center：中心结点，是一个对象， children：子结点，是一个数组【{text：，line: ,src：}】,  scene:场景
function family(center, children, radius, color){
    let angle = Math.PI * 2 / children.length;
    for (let k=0, len=children.length; k < len; k++){
        // 计算位置
        let px = center.x + radius * Math.sin(k * angle);
        let py = center.y + radius * Math.cos(k * angle);
        let p = [{text: children[k].text, src: children[k].src}];
        let status = father_or_not(center,children[k].text)
        if (status ==3){
            let node = make_node(p,  [{x: px, y:py}] , 25, color)[0];
            node.radius = 25;
            node.fillColor = color;
            //node.dragable = false;
            // 创建连接
            let line = new JTopo.Link(center, node);
            line.text = children[k].line;
            line.strokeColor = "255,255,102";
            scene.add(line);
            // 记录结点和连线
            vary_line.push(line);
        } else if (status == 2){
//        返回该结点，并建立联系
            let node_now = vary_node[children[k].text]
            let line = new JTopo.Link(center, node_now);
            line.text = children[k].line;
            line.strokeColor = "255,255,102";
            scene.add(line);
//            记录关系
            vary_line.push(line);
        }
    }
}
// data: 是一个数组【{text：，src：}】, postion: 是一个数组【{x：，y：}】
function make_node(data, position, radius, color){
    let nodes = [];
    for (let k=0, len=data.length; k < len; k++){
        let node = new JTopo.CircleNode( data[k].text);
        scene.add(node);
        Object.defineProperty(vary_node, node.text , {value : node,writable : true,enumerable : true,configurable : true});
//        记录结点
        nodes.push(node)
        //node.dragable = false;
        node.setLocation(position[k].x, position[k].y);
        //  填充的颜色
        node.fillColor = color;
        node.selected = true;
        node.textPosition = 'Middle_Center';
        //  圆形结点的半径
        node.radius = radius;
        // 设置点击响应函数
        let src = data[k].src;
        //node.addEventListener("dbclick", function(){  if (src != ''){ window.open(src); } });
        // 点击时获得子结点
        node.addEventListener("dbclick", function(event){
//            let open = confirm("是否要打开详情页？", "选择");
//            if (open) {window.open(src); }
            if (all_children(event.target).length != 0){
                remove(event.target);
                // 关闭时没有阴影
                event.target.shadow = false;
            } else {
            //发出请求
            get_data(event.target)
            }
        });
    }
    return nodes;
}
// 新增结点有三种情况：一种是父结点已经存在该子结点，第二种是该结点已经存在,是其他父结点的子结点, 第三种是该结点没有存在
function father_or_not(center, text){
//  新增结点不是中心结点本身
    if (center.text == text){ return 1}
    if(vary_node[text]==undefined){
        return 3;
    } else {
        let children_nodes = all_children(center)
//        该中心结点有子结点
        if (children_nodes.length != 0){
            for (let i=0, len=children_nodes.length; i<len; i++){
                if (children_nodes[i].text == text){ return 1;}
            }
        return 2;
        }
        return 2
    }
}

// 递归调用，删除子孙级别的结点
function remove(target){
    let children_nodes = [];
//    记录将要删除关系的索引
    let index = [];
    for (let i=0, len=vary_line.length; i<len; i++){
        if (vary_line[i].nodeA == target){
            children_nodes.push(vary_line[i].nodeZ);
            index.push(i);
        }
    }
    for (let i=0, len=children_nodes.length;i<len;i++){
        remove(children_nodes[i])
    }
 //    删除关系
    for (let i=0, len=index.length;i<len;i++){
        scene.remove(vary_line[index[i]])
    }
//   解除关系后和将子结点删除: 条件是只有一个父结点
    for (let i=0, len=children_nodes.length;i<len;i++){
        if (all_father(children_nodes[i]).length == 1){
            delete vary_node[children_nodes[i].text]
            scene.remove(children_nodes[i])
        }
    }
    for (let i=0, len=index.length;i<len;i++){
            vary_line.splice(index[i]-i, 1)
    }
}
// 获得某中心结点的所有子结点
function all_children(center){
    let children_nodes = []
    for (let i=0, len=vary_line.length; i<len; i++){
        if (vary_line[i].nodeA == center){children_nodes.push(vary_line[i].nodeZ)}
    }
    return children_nodes
}
function all_father(center){
    let father_nodes = []
    for (let i=0, len=vary_line.length; i<len; i++){
        if (vary_line[i].nodeZ == center){father_nodes.push(vary_line[i].nodeA)}
    }
    return father_nodes
}
// 设置请求子结点函数
function get_data(target){
    let data = {node: target.text};
    $.post('/get_children', data, function (result) {
        let children = []
        result = JSON.parse(result)
        for (let key in result){
            children.push(result[key])
        }
        let nodes = family(target, children, 150, "255,102,102");
        // 展开的设置阴影
        target.shadow = true;
    })
}
//result = result.split("|");
var result = ['深度的'];
//获得画布

var canvas = document.getElementById('canvas');

var stage = new JTopo.Stage(canvas); // 创建一个舞台对象
var scene = new JTopo.Scene(stage); // 创建一个场景对象

var all_node = {} // 存储各级结点， {描述名：子结点数组}
var vary_node = {} //存储各个结点， {描述名：结点对象}
var vary_line = [] //存储各种关系

for (let i=0,len=result.length; i<len; i++ ){
    let data = [{text: result[i], src:"https://baike.baidu.com/item/%E5%A4%A7%E8%B1%86/567793?fr=aladdin"}];
    let postion = [{x:canvas.width * 0.5 - i*25, y:canvas.height * 0.5 - i*25}];
    make_node(data, postion, 25, '255,102,102');
}




